<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="arr" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <template v-slot="{data}">
          <el-row class="row">
          <el-col>{{ data.name }}</el-col>
          <el-col :span="4">
          <span class="tree-manager">{{ data.managerName }}</span>
          <el-dropdown @command="operateDepart($event, data.id)">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          </el-col>
        </el-row>
        </template>
      </el-tree>
    </div>
    <!-- .sync修饰后在子组件中只需要通过触发this.$emit('update:showDialog', 值)就可以同步修改父组件中的数据 -->
    <addDept ref="addDep" @updateData="getDepartmenmtData" :show-dialog.sync="showDialog" :currentNodeId="currentNodeId"></addDept>
  </div>
</template>

<script>
import {getDepartmentList, deleteDepartment} from '@/api/department'
import {transListToTree} from '@/utils/index'
import addDept from './component/add-dept.vue'
export default {
  name: 'Department',
    data() {
      return {
       arr: [{
        name: '传智教育',
        manageName: '管理员',
        children: []
      }],
        defaultProps: {
          label: 'name',
          children: 'children'
        },
        showDialog: false,
        currentNodeId: 0
      }
    },
    methods: {
      // 获取部门数据
      async getDepartmenmtData() {
        const res = await getDepartmentList()
        console.log(res);
        const data = transListToTree(res, 0)
        console.log(data);
        this.arr = data                                   
      },
      operateDepart(command, id) {
        // 添加子部门
        if (command === 'add') {
          console.log(command, id);
          this.showDialog = true
          this.currentNodeId = id
        }
        // 编辑部门
        else if (command === 'edit') {
          this.currentNodeId = id
          this.showDialog = true

          // 获取组件实例
          this.$nextTick(() => {
            this.$refs.addDep.getDepartmentDetail()
          })
        }
        else if (command === 'del') {
          // 弹出框提示是否删除
          this.$confirm('此操作将永久删除该部门, 是否继续?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // 确定逻辑

          await deleteDepartment(id) // 删除部门
          // 提示消息
          this.$message({  
            type: 'success',
            message: '删除成功!'
          });
          // 重新获取数据渲染
          this.getDepartmenmtData()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
        }
       }
    },
    created() {
      this.getDepartmenmtData()
    },
    components: {
      addDept
    }
}
</script>

<style scoped >
  .app-container {
    padding: 30px 140px;
    font-size: 14px;
  }
  .row {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    line-height: 40px;
  }
  .tree-manager {
    /* width: 50px; */
    display: inline-block;
    margin: 10px;
  }
</style>